//
//  ViewController.m
//  UI_17_sandBox
//
//  Created by li cheng on 16/8/5.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "Book.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 沙盒(sandBox)
    // Documents(文件文档, 用户数据存储)
    // Library(资源,一般用来存放,程序员要存储的一些数据)
    //                👇
    //          Caches(缓存文件)
    //          Preferences(用户信息, 和用户的一些设置, NSUserDefaults)
    // temp(下载的临时文件一般放这里)
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // 2. 获取沙盒路径
    NSLog(@"%@", NSHomeDirectory());
    //快捷获取到对应目录的 C函数
    NSHomeDirectory();  //根目录
    NSTemporaryDirectory(); //临时目录
    NSLog(@"Home-------%@",NSHomeDirectory());
    NSLog(@"temporary========%@",NSTemporaryDirectory());
    //C函数
    //参数1:搜索那个文件夹的路径
    //参数2.在 用户 作用域下搜索
    //参数3: Yes代表绝对路径,NO代表相对路径,基本使用绝对路径
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"PathArray++++++++%@",[pathArray firstObject]);
    // NSBundle
    NSLog(@"%@", [NSBundle mainBundle]);
    // 1.简单的文件读写 InPut InOut
    NSString *helloString = @"Hell, I/O file";
//    NSString *writePath = [[pathArray firstObject] stringByAppendingString:@"/hello.txt"];
    NSString *writePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello.txt"];
    NSError *error = nil;
    BOOL isSuccess = [helloString writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (isSuccess) {
        NSLog(@"存储失败");
    } else {
        NSLog(@"恭喜存储成功");
    }
    // 2> 读取路径对应的文字
    NSError *readError = nil;
    NSString *readString = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@", readString);
    // 3> 将数组写入本地文件
    NSArray *array = @[@"黄旭", @"王焕", @"的手机卡了"];
    NSString *arrayPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
    [array writeToFile:arrayPath atomically:YES];
    BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES];
    if (isArrayWriteSuccess) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    // 4> 将 数组 读入
    NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"----%@", nameArray);
    
    // 5> 字典的存入
    NSString *dicPath = [[pathArray firstObject] stringByAppendingPathComponent:@"dic.plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"libai", @"name", @"man", @"sex", nil];
    BOOL isDicWriteSuccess = [dic writeToFile:dicPath atomically:YES];
    if (isDicWriteSuccess) {
        NSLog(@"字典存进去了");
    } else {
        NSLog(@"字典没哟存进去了");

    }
    // 6> 取出字典
    NSDictionary *findDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@", findDic);
    // 7> 将Data类型写入本地
    UIImage *Image = [UIImage imageNamed:@"0"];
    // 后面的参数是压缩
    //    UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)
    NSData *imageData = UIImageJPEGRepresentation(Image, 0.1);
//    NSLog(@"%@", imageData);
    
    NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageDate"];
    BOOL isDataWriteSuccess = [imageData writeToFile:dataPath atomically:YES];
    if (isDataWriteSuccess) {
        NSLog(@"data写入成功");
    } else {
        NSLog(@"data写入失败");
    }
//    UIImage *findImage = [UIImage imageWithData:imageData];
    // 8> NSData 类型 读入
    NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath];
    UIImage *fileImage = [UIImage imageWithData:imageNewData];
    NSLog(@"%@", fileImage);
    // 2.复杂对象文件读写,自定义类型
    // 归档/反归档, 序列化/反序列化
    
    // 1> 归档, 将 对象存入本地
    Book *book = [Book new];
    book.bookName = @"iOS从入门到放弃";
    book.bookType = @"教育";
    book.bookPrice = @"98.88";
    book.bookAuthor = @"王焕";
    book.bookAddress = @"王焕飞可急出版社";
    NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
    BOOL isBookWri = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBookWri) {
        NSLog(@"chengongle");
    } else {
        NSLog(@"emiyou");
    }
    // 2> 反归档
    Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"%@", huangBook.bookAuthor);
    
    // 如果对象想要实现归档和反归档
    // 1> 对象对应的类需要签订<nsCoding>
    // 2> 实现协议方法
    //    1> initWithCoder 反归档使用
    //    2> encodeWithCoder 归档用
    // 3.归档时, 使用 NSKeyedArchiver
    // 4.反归档时, 使用 NSKeyedArchiver
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
